#/usr/bin/bash
INSTALL_PATH=$1
if [ ! $INSTALL_PATH ];then
	INSTALL_PATH="/usr/local/mysql"
fi

#MYSQL架包下载路径,不传默认ftp://ftpUser:123456@175.178.190.189/mysql/mysql-5.7.26/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
DOWNLOAD_URL=$2
if [ ! $DOWNLOAD_URL ];then
	DOWNLOAD_URL="ftp://ftpUser:123456@175.178.190.189/mysql/mysql-5.7.26/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz";
fi

#MYSQL端口，不传默认为3306
MYSQL_PORT=$3
if [ ! $MYSQL_PORT ];then
	MYSQL_PORT="3306"
fi

#MYSQL密码，不传默认为123456
MYSQL_PASSWORD=$4
if [ ! $MYSQL_PASSWORD ];then
	MYSQL_PASSWORD="123456"
fi

##检测系统是否安装MySQL
rpm -qa | grep -i mysql

if [ $? = 0 ];then
	echo "------------卸载MySQL-------------"
	rpm -e --nodeps `rpm -qa | grep -i  mysql`
fi

#判断是否存在mysql进程，有则先kill掉
ps -ef | grep mysql.pid | grep -v grep | awk '{print $2}' | xargs kill -9
#PROCESS=`ps -ef|grep mysql.pid|grep -v grep.|grep -v PPID|awk '{print $2}'`
#for i in $PROCESS
# do
#  echo "kill the mysql process [$i]"
#  kill -9 $i
# done



##获取文件名
FILENAME=${DOWNLOAD_URL##*/};

#如果文件夹不存在，创建文件夹
if [ ! -d $INSTALL_PATH ];then
   mkdir $INSTALL_PATH
fi

cd $INSTALL_PATH

##下载安装架包
wget $DOWNLOAD_URL

#判断当前文件是什么压缩包
if [ ${FILENAME##*.} = gz ];then
  TEMP=${FILENAME%.*}
    if [ ${TEMP##*.} = tar ];then
        tar -zxvf $FILENAME -C $INSTALL_PATH
    else
        unzip  $FILENAME -d  $INSTALL_PATH
    fi
elif [ ${FILENAME##*.} = zip ];then
    unzip  $FILENAME -d  $INSTALL_PATH
fi



#删除安装包
rm -rf $INSTALL_PATH/$FILENAME

#获取解压后当前目录下的文件名
NEW_FILENAME=`ls -lt $INSTALLPATH |grep mysql|head -n 1|awk '{print $9}'`;


if [ ! $NEW_FILENAME ];then
  echo "解压失败，获取不到解压后的文件名，退出脚本";
  exit
fi


#检查是否有mysql用户组和mysql用户,没有就添加
groups mysql 

if [ $? -ne 0 ];then
	echo '----------创建mysql用户组-------------'
	groupadd mysql && useradd -r -g mysql mysql
fi

if [ ! -d "/data/mysql" ];then
	mkdir -p /data/mysql
fi

if [ ! -d "/var/lib/mysql" ];then
	mkdir -p /var/lib/mysql
fi 

if [ ! -d "/var/run/mysql" ];then
	mkdir -p /var/run/mysql
fi


rm -rf /data/mysql/*
rm -rf /var/lock/subsys/mysql

chown mysql:mysql -R /data/mysql
chown -R mysql:mysql /var/run/mysql
chown mysql:mysql -R /var/lib/mysql

if [  -f "/etc/my.cnf" ];then
	rm -rf /etc/my.cnf;
fi

cat >>/etc/my.cnf<<EOF
[mysqld]
bind-address=0.0.0.0
port=$MYSQL_PORT
user=mysql
basedir=$INSTALL_PATH/$NEW_FILENAME
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
skip-grant-tables=1
[client]
socket=/var/lib/mysql/mysql.sock
EOF

cd $INSTALL_PATH/$NEW_FILENAME

./bin/mysqld --defaults-file=/etc/my.cnf --basedir=$INSTALL_PATH/$NEW_FILENAME --datadir=/data/mysql/ --user=mysql --initialize

cp $INSTALL_PATH/$NEW_FILENAME/support-files/mysql.server /etc/init.d/mysqld
cp $INSTALL_PATH/$NEW_FILENAME/bin/mysql /usr/bin

service mysqld start


if [ $? -ne 0 ]; then
	echo "mysql启动失败，请排查原因，退出脚本";
        exit;
fi

rpm -qa | grep -i expect

if [ $? -ne 0 ];then
	yum install -y expect
fi

expect << EOF
    set timeout -1spawn
    spawn mysql -uroot -p
    expect "*password*" {send "\r"}
    expect "*>*" {send "use mysql;\n"}
    expect "*>*"  {send "update user set host = '%' where user = 'root';\n"}
    expect "*>*"  {send "FLUSH PRIVILEGES;\n"}
    expect "*>*" {send "ALTER USER 'root'@'%' IDENTIFIED  BY '$MYSQL_PASSWORD';\n"}
    expect "*>*" {send "FLUSH PRIVILEGES;\n"}
    expect "*>*" {send "quit;\n"}  
EOF



cd /etc

sed -i '/skip-grant-tables=1/d' my.cnf

#设置mysql开机启动
cd /etc/init.d
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

service mysqld restart

if [ $? -ne 0 ]; then
    echo "mysql启动失败，请排查原因，退出脚本";
    exit;
else
    echo "mysql 启动成功，密码为$MYSQL_PASSWORD，请稍后手动登录云服务器,在安全组中放通$MYSQL_PORT端口";
fi

